package com.distributeredis.redis_springboot.springeasyexcel.readtest.read;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.distributeredis.redis_springboot.springeasyexcel.readtest.vo.DemoData;
import org.junit.Test;

public class ReadTest {
    String PATH = "H:\\LUA\\redis_springboot\\src\\main\\java\\com\\distributeredis\\redis_springboot\\springeasyexcel\\readtest\\user9.xlsx";
    // 方式一
    @Test
    public void test() {
        // 读取excel
        EasyExcel.read(PATH, DemoData.class, new AnalysisEventListener<DemoData>() {
            /**
             * 每解析一行excel表格数据，就会被调用一次
             * @param demoData
             * @param analysisContext
             */
            @Override
            public void invoke(DemoData demoData, AnalysisContext analysisContext) {
                System.out.println("解析数据为："+demoData);
            }
            /**
             * 全部解析完被调用
             * @param analysisContext
             */
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("全部解析完被调用！");
            }
        }).sheet(0).doRead();
    }
    //方式二
    @Test
    public void test1() {
        // 读取excel
        ExcelReader excelReader = EasyExcel.read(PATH, DemoData.class, new AnalysisEventListener<DemoData>() {
            /**
             * 每解析一行excel表格数据，就会被调用一次
             * @param demoData
             * @param analysisContext
             */
            @Override
            public void invoke(DemoData demoData, AnalysisContext analysisContext) {
                System.out.println("解析数据为：" + demoData);
            }

            /**
             * 全部解析完被调用
             * @param analysisContext
             */
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("全部解析完被调用！");
            }
        }).build();
        ReadSheet readSheet = EasyExcel.readSheet(0)
                .build();
        excelReader.read(readSheet);
        // 关闭流
        excelReader.finish();
    }

    /**
     * 读取全部sheet
     */
    @Test
    public void test2() {
        // 读取excel
        EasyExcel.read(PATH, DemoData.class, new AnalysisEventListener<DemoData>() {
            /**
             * 每解析一行excel表格数据，就会被调用一次
             * @param demoData
             * @param analysisContext
             */
            @Override
            public void invoke(DemoData demoData, AnalysisContext analysisContext) {
                System.out.println("解析数据为："+demoData);
            }
            /**
             * 全部解析完被调用
             * @param analysisContext
             */
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("全部解析完被调用！");
            }
        }).doReadAll();
    }

    /**
     * 读取excel中的某几个sheet
     */
    @Test
    public void test4() {
        // 读取excel 构建excelReader对象
        ExcelReader excelReader = EasyExcel.read(PATH).build();
        // 构建sheet标签对象
        ReadSheet readSheet = EasyExcel.readSheet(0).head(DemoData.class).registerReadListener(new AnalysisEventListener<DemoData>() {
            @Override
            public void invoke(DemoData demoData, AnalysisContext analysisContext) {
                System.out.println("解析数据为："+demoData);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("全部解析完被调用！");
            }
        }).build();

        ReadSheet readSheet1 = EasyExcel.readSheet(1).head(DemoData.class).registerReadListener(new AnalysisEventListener<DemoData>() {
            @Override
            public void invoke(DemoData demoData, AnalysisContext analysisContext) {
                System.out.println("解析数据为："+demoData);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("全部解析完被调用！");
            }
        }).build();

        excelReader.read(readSheet,readSheet1);
        excelReader.finish();
    }

}
